#!/bin/bash

# 返回码
declare -i ret_ok=0                                       # 成功
declare -i ret_failed_config_file_not_exist=1             # 失败 配置文件不存在
declare -i ret_failed_ha_path_invalid=2                   # 失败 ha安装目录无效或未制定
declare -i ret_failed_fd_args_invalid=3                   # 失败 FD参数设置非法
declare -i ret_failed_fd_set_pass_python_failed=4         # 失败 FD参数设置密错误
declare -i ret_failed_ief_args_invalid=5                  # 失败 IEF参数设置非法
declare -i ret_failed_edge_main_process_runing=6          # 失败 检测到edge_main进程运行
declare -i ret_failed_set_ha_config_failed=7              # 失败 set ha config failed
declare -i ret_failed_import_fd_cert_failed=8             # 失败 导入FD证书失败
declare -i ret_failed_set_third_hardware_info_failed=9    # 失败 set third hardware info failed
declare -i ret_failed_ief_container_exist=10              # 失败 切换IEF之前有容器存在
declare -i ret_failed_fd_test_check=11                    # 失败 check FD user and pword
declare -i ret_failed_set_web_config_failed=12            # 失败 set web config failed
declare -i ret_failed_A500_not_support_config_web=13      # 失败 A500 不支持 enable/disable web

WORK_PATH="/opt/middleware/AtlasEdge"
SCRIPT_PATH="/opt/middleware/AtlasEdge/software/edge_site/edge_manager/src/script"
SITE_PYTHON_PATH="${WORK_PATH}/software/edge_site/edge_manager/src/python/"

ATLASEDGE_CONFIG_FILE="${WORK_PATH}/config/config.ini"
ATLASEDGE_NODE_INFO_FILE="${WORK_PATH}/config/NODE_INFO"
ATLASEDGE_CORE_CONFIG_FILE="${WORK_PATH}/software/edge_core/conf/edgecore.yaml"
THIRD_WHITE_LIST="${WORK_PATH}/config/3rd_config.ini"
LIMIT_SIZE_10M=10485760
LIMIT_SIZE_1M=1048576
PYTHON3=$(type -p python3)

. ${SCRIPT_PATH}/log_util.sh
. ${SCRIPT_PATH}/common.sh

function usage() {
    echo "config  -- set software config"
    echo "parameters:"
    echo "    --manufacturer : "
    echo "    --product_name : "
    echo "    --install_type : Full Typical Simple"
    echo "    --netmanager : Web FD IEF"
    echo "    --fd_test : fd account and pword check. true or false"
    echo "    --fd_ip : fd netmanager require"
    echo "    --fd_user : fd netmanager require"
    echo "    --ief_cert: ief netmanager require "
    echo "    --fd_cert_import: FD netmanager require "
    echo "    --ha_work_path: HA installl path "
    echo "    --user_log_path: user log path "
    echo "    --collect_log_size: size of upload log file"
    echo "    --no_sys_load_mode: start as system service. 1(yes) or 0(no) "
    echo "    --ha_enable: enable/disable HA. 1(enable) or 0(disable) "
    echo "    --web_enable: enable/disable WebUI. 1(enable) or 0(disable) "
    echo "    --third_hardware_info_en: enable/disable third hardware manager. 1(enable) or 0(disable) "
    echo "    --whitelist:whitelist add, Separate process names with space"
    echo "    --h : help"
    echo "usage: "
    echo "    config --manufacturer=huawei --product_name='Atlas 500'"
    echo "    config --fd_test=true --netmanager=FD --fd_user=user --fd_ip=IP Address"
    echo "    config --ha_enable=1 --ha_work_path=/xxx/xxx/ha"
    echo "    config --web_enable=0"
}

function check_ip_valid() {
    IP=$1
    VALID_CHECK=$(echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
    if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$">/dev/null; then
        if [ ${VALID_CHECK:-no} == "yes" ]; then
            logger_Debug "IP $IP available."
            return 0
        else
            logger_Warn "IP $IP not available!"
            return 1
        fi
    else
        logger_Warn "IP format error!"
        return 1
    fi
}

function third_check(){
    if [ ! -f "${THIRD_WHITE_LIST}" ]; then
        touch ${THIRD_WHITE_LIST}
    fi
    chmod 600 ${THIRD_WHITE_LIST}
    while [ ${2} ]
    do
        sign=true
        for lines in `cat ${THIRD_WHITE_LIST}`
        do
            if [ ${2} == $lines ]; then
                echo $2" already exists"
                sign=false
            fi
        done
        if [ "${2}" != "--" ] && [ "${2}" != "-" ] && [ ${sign} == true ]; then
            echo "${2}" >> ${THIRD_WHITE_LIST}
        fi

    shift
    done
}

function is_integer_check(){
    if [ -n "$(echo $1| sed -n "/^[0-9]\+$/p")" ] && [ $1 -gt 0 ]; then
        echo $1 "is a qualified value"
    else
        echo $1" is not an integer greater than 0"
        exit
    fi
}

check_args_valid(){
#程序如果开启 是否禁止设置

    # 安装模式为Full模式下必须要按照HA传递HA工作路径，检测HA工作路径是否合法
    if [ "${HA_ENABLE}" == "1" ]; then
        if [ ! -d ${HA_WORK_PATH} ]; then
            logger_Warn "Full Install type need HA path invalid [$(basename ${HA_WORK_PATH})]"
            return ${ret_failed_ha_path_invalid}
        elif  [ -z "${HA_WORK_PATH}" ]; then
            logger_Warn "Full Install type need HA path null [$(basename ${HA_WORK_PATH})]"
            return ${ret_failed_ha_path_invalid}
        fi
    fi

    if [ "${WEB_ENABLE}" != "" ]; then
    	grep -q brd_t=sei /proc/cmdline 2>/dev/null
        if [[ $? == 0 ]]; then
            logger_Warn "A500 not support config web"
            return ${ret_failed_A500_not_support_config_web}
        fi
    fi

    if [ "${NET_MANAGER}" == "FD" ]; then
        if [ -z "${FD_USER}" -o -z "${FD_IP}" ]; then
            logger_Warn "FD netType user or ip invalid"
            return ${ret_failed_fd_args_invalid}
        fi
        #检测ip是否合法有效
        check_ip_valid ${FD_IP}
        ret=$?
        if [ ${ret} != 0 ];then
            logger_Warn "FD netType user pword ip invalid"
            return ${ret_failed_fd_args_invalid}
        fi

        read -t 30 -p "Enter FD Pword:" -s FD_PWORD
        ret=$?
        if [ ${ret} != 0 ];then
            logger_Warn "input pword timeout exit"
            return ${ret_failed_fd_args_invalid}
        fi
        if [ ${#FD_USER} -lt 0 -o ${#FD_USER} -gt 256 ]; then
            logger_Warn "invalid fd_user len:${#FD_USER} not valid [0-256]"
            return ${ret_failed_fd_args_invalid}
        fi
        # check pass valid
        if [ ${#FD_PWORD} -lt 8 -o ${#FD_PWORD} -gt 256 ]; then
            logger_Warn "invalid pword len:${#FD_PWORD} not valid [8-256]"
            return ${ret_failed_fd_args_invalid}
        fi
    fi

    # ief 模式检测证书路径是否有效
    if [ "${NET_MANAGER}" == "IEF" ]; then
        if [ -z "${IEF_CERT}" ]; then
            logger_Warn "ief nettype need cert path null [$(basename ${IEF_CERT})]"
            return ${ret_failed_ief_args_invalid}
        elif [ ! -f ${IEF_CERT} ]; then
            logger_Warn "ief nettype need cert path invalid [$(basename ${IEF_CERT})]"
            return ${ret_failed_ief_args_invalid}
        fi
        check_IEF_param
        if [ $? != 0 ];then
            logger_Warn "ief param inviald [${IEF_CERT}}"
            return ${ret_failed_ief_args_invalid}
        fi
    fi

    return ${ret_ok}
}

set_config_to_file() {

    # 声明环境变量 用于python调用
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${WORK_PATH}/software/lib/
    export PYTHONPATH=$PYTHONPATH:${WORK_PATH}/software/edge_site/opensource:${WORK_PATH}/software/edge_site
    export ATLASEDGE_WORK_PATH=${WORK_PATH}
    export ATLASEDGE_CONFIG_PATH=${WORK_PATH}/config
    export ATLASEDGE_SOFTWARE_PATH=${WORK_PATH}/software

    if [ "${FD_TEST}" == "true" ]; then
        ret=`echo ${FD_IP} ${FD_USER} ${FD_PWORD}| ${PYTHON3} ${WORK_PATH}/software/edge_site/postern/restful_net_requests.py`
        if [ $? != 0 ]; then
            logger_Error "check fd user or pword failed: $ret"
            return ${ret_failed_fd_test_check}
        fi
        logger_Info "check fd user or pword OK "
    fi

    if [ "${INSTALL_TYPE}" !=  "" ]; then
        WriteINIfile ${ATLASEDGE_CONFIG_FILE} SYS install_type ${INSTALL_TYPE}
        logger_Info "set install_type:${INSTALL_TYPE} OK "

        set_install_type ${INSTALL_TYPE} ${WORK_PATH}
    fi

    if [ "${HA_WORK_PATH}" !=  "" ]; then
        WriteINIfile ${ATLASEDGE_CONFIG_FILE} SYS ha_work_path ${HA_WORK_PATH}
        logger_Info "set ha_work_path:$(basename ${HA_WORK_PATH}) OK "
    fi

    # set ha info
    if [ "${HA_ENABLE}" !=  "" ]; then
        if [ "${HA_ENABLE}" == "1" ]; then
            sed -i "s|is_enabled=.*|is_enabled=True|g" ${WORK_PATH}/software/edge_site/ha_handler/config/config.ini
            ${PYTHON3} ${SITE_PYTHON_PATH}/config_capability.py ha_config enable
            ret=$?
        else
            sed -i "s|is_enabled=.*|is_enabled=False|g" ${WORK_PATH}/software/edge_site/ha_handler/config/config.ini
            ${PYTHON3} ${SITE_PYTHON_PATH}/config_capability.py ha_config disable
            ret=$?
        fi
        if [ $ret != 0 ];then
            logger_Warn "ha config ${HA_ENABLE} set failed ret:$ret"
            return ${ret_failed_set_ha_config_failed}
        fi
    fi

    # set web(flask) info
    if [ "${WEB_ENABLE}" !=  "" ]; then
        if [ "${WEB_ENABLE}" == "1" ]; then
            sed -i "s|is_enabled=.*|is_enabled=True|g" ${WORK_PATH}/software/edge_site/postern/config/config.ini
            ret=$?
        else
            sed -i "s|is_enabled=.*|is_enabled=False|g" ${WORK_PATH}/software/edge_site/postern/config/config.ini
            ret=$?
        fi
        if [ $ret == 0 ];then
            logger_Info "web config ${WEB_ENABLE} set OK "
        else
            logger_Warn "web config ${WEB_ENABLE} set failed ret:$ret"
            return ${ret_failed_set_web_config_failed}
        fi
    fi

    if [ "${THIRD_HARDWARE_INFO_EN}" != "" ]; then
        if [ "${THIRD_HARDWARE_INFO_EN}" == "1" ]; then
            ${PYTHON3} ${SITE_PYTHON_PATH}/config_capability.py third_hardware_info_en enable
            ret=$?
        else
            ${PYTHON3} ${SITE_PYTHON_PATH}/config_capability.py third_hardware_info_en disable
            ret=$?
        fi
        if [ $ret != 0 ];then
            logger_Warn "third hardware info enable ${THIRD_HARDWARE_INFO_EN} set failed ret:$ret"
            return ${ret_failed_set_third_hardware_info_failed}
        fi
    fi

    if [ "${USER_LOG_PATH}" !=  "" ]; then
        WriteINIfile ${ATLASEDGE_CONFIG_FILE} SYS user_log_path ${USER_LOG_PATH}
        logger_Info "set user_log_path:$(basename ${USER_LOG_PATH}) OK "
    fi

    if [ "${COLLECT_LOG_SIZE}" !=  "" ]; then
        WriteINIfile ${ATLASEDGE_CONFIG_FILE} SYS collect_log_size ${COLLECT_LOG_SIZE}
        logger_Info "set collect_log_size:${COLLECT_LOG_SIZE} OK "
    fi

    if [ "${MANUFACTURER}" !=  "" ]; then
        sed -i "s|manufacturer:.*|manufacturer: ${MANUFACTURER}|g" ${ATLASEDGE_NODE_INFO_FILE}
        logger_Info "set manufacturer:${MANUFACTURER} OK "

    fi

    if [ "${PRODUCT_NAME}" !=  "" ]; then
        sed -i "s|node_type:.*|node_type: ${PRODUCT_NAME}|g" ${ATLASEDGE_NODE_INFO_FILE}
        WriteDevMgmtTypeToYaml ${ATLASEDGE_CORE_CONFIG_FILE} "${PRODUCT_NAME}"

        update_serial_num ${ATLASEDGE_CORE_CONFIG_FILE}

        logger_Info "set product name:${PRODUCT_NAME} OK "
    fi

    if [ "${FD_USER}" !=  "" ]; then
        sed -i "s|clouduser:.*|clouduser: ${FD_USER}|g" ${ATLASEDGE_CORE_CONFIG_FILE}
        logger_Info "save fd user:${FD_USER} OK "

    fi

    if [ "${FD_PWORD}" !=  "" ]; then
        # 待完善
        out_pword=`echo ${FD_PWORD} | ${WORK_PATH}/software/libs/ld-linux-aarch64.so.1 --library-path ${WORK_PATH}/software/libs/ $WORK_PATH/software/bin/encrypt_code 1 `
        ret=$?
        if [ ${ret} != 0 ];then
            logger_Warn "FD  pword set failed "
            return ${ret_failed_fd_set_pass_python_failed}
        fi
        logger_Info "the node encrypt the FD pwod success, and will save to yaml config"
        sed -i "s|cloudpword:.*|cloudpword: ${out_pword}|g" ${ATLASEDGE_CORE_CONFIG_FILE}
        logger_Info "save fd pword OK "
        unset FD_PWORD
    fi

    if [ "${FD_IP}" !=  "" ]; then
        # 写入ip信息
        ${PYTHON3} ${SITE_PYTHON_PATH}/FDIpProc.py ${FD_IP}
        WriteINIfile ${ATLASEDGE_CONFIG_FILE} SYS net_manager_address ${FD_IP}
        logger_Info "set fd ip:${FD_IP} OK "
    fi

    if [ "${NET_MANAGER}" !=  "" ]; then
        sed -i "s|net_manager:.*|net_manager: ${NET_MANAGER}|g" ${ATLASEDGE_NODE_INFO_FILE}
        logger_Info "set net_manager:${NET_MANAGER} OK "

        if [ "${NET_MANAGER}" !=  "FD" ]; then
            # clear fd info
            sed -i "s|clouduser:.*|clouduser: |g" ${ATLASEDGE_CORE_CONFIG_FILE}
            sed -i "s|cloudpword:.*|cloudpword: |g" ${ATLASEDGE_CORE_CONFIG_FILE}
        fi
    fi

    if [ "${IEF_CERT}" !=  "" ]; then
        # check container exit
        docker ps | grep k8s_POD
        ret=$?
        if [ $ret == 0 ];then
            logger_Warn "the k8s container exist ,please delete"
            return ${ret_failed_ief_container_exist}
        elif [ $ret != 1 ];then
            logger_Warn "excute docker ps command failed,ret:"$ret
            return ${ret_failed_ief_container_exist}
        fi
        # copy ief certfile to dir
        if [ ! -d  /run/web/ ]; then
            mkdir -p  /run/web/
        fi
        chown HwHiAiUser:HwHiAiUser /run/web/
        cp ${IEF_CERT} /run/web/ief_cert.tar.gz

        SITE_EDGEMANAGE_CONFIG="${WORK_PATH}/software/edge_site/edge_manager/config/config.ini"
        if [ ! -f "${SITE_EDGEMANAGE_CONFIG}" ]; then
            logger_Warn "get edgemanager config failed"
        fi

        dns_resolution_address_info=$(cat ${SITE_EDGEMANAGE_CONFIG} | grep dns_resolution_address)
        if [ ! "${dns_resolution_address_info}" ]; then
            logger_Warn "get dns_resolution_address_info failed"
        fi

        dns_resolution_address=${dns_resolution_address_info#*=}
        if [ "${dns_resolution_address}" != "" ] && [[ "${dns_resolution_address}" =~ ";" ]]; then
            dns_resolution_address_first=${dns_resolution_address%;*}
            dns_resolution_address_second=${dns_resolution_address#*;}
            # add dns info. IP address of the DNS server used by the edge device to resolve the IEF domain name.
            cat /etc/resolv.conf | grep "${dns_resolution_address_first}" >/dev/null
            if [ $? != 0 ];then
                # IP address of the DNS server used by the edge device to resolve the IEF domain name.
                echo "${dns_resolution_address_first}" >> /etc/resolv.conf
            fi
            # IP address of the DNS server used by the edge device to resolve the IEF domain name.
            cat /etc/resolv.conf | grep "${dns_resolution_address_second}" >/dev/null
            if [ $? != 0 ];then
                # IP address of the DNS server used by the edge device to resolve the IEF domain name.
                echo "${dns_resolution_address_second}" >> /etc/resolv.conf
            fi
            logger_Info "set ief cert :$(basename ${IEF_CERT}) OK "
        fi
    fi

    if [ "${NO_SYS_LOAD_MODE}" !=  "" ]; then
        WriteINIfile ${ATLASEDGE_CONFIG_FILE} SYS no_sys_load_mode ${NO_SYS_LOAD_MODE}
        logger_Info "set no_sys_load_mode:${NO_SYS_LOAD_MODE} OK "
    fi

    if [ "${FD_CERT_IMPORT}" !=  "" ]; then
        if [ -f ${FD_CERT_IMPORT} ]; then
            check_FD_param
            if [ $? != 0 ];then
                logger_warn "FD cert import check paramter failed"
                return ${ret_failed_import_fd_cert_failed}
            fi
            import_fd_cert ${FD_CERT_IMPORT}
            ret=$?
            if [ ${ret} != 0 ];then
                logger_warn "FD cert import fail"
                return ${ret_failed_import_fd_cert_failed}
            fi
            logger_Info "FD cert import:${FD_CERT_IMPORT} OK"
        fi
    fi

}

check_IEF_param(){
    file_size=`ls -l ${IEF_CERT} | awk '{print $5}'`
    if [ $file_size -gt $LIMIT_SIZE_10M ] || [ $file_size -le 0 ];then
        logger_Warn "IEF file size over greater than limit"
        return ${ret_failed_ief_args_invalid}
    fi
    suffix=`echo ${IEF_CERT} | sed 's/.*\(......\)$/\1/'`
    if [ $suffix != "tar.gz" ];then
        logger_Warn "IEF path invalid"
        return ${ret_failed_ief_args_invalid}
    fi
    check_package_inner_file ${IEF_CERT}
    if [ $? != 0 ];then
        logger_Warn "IEF inner file name invalid"
        return ${ret_failed_ief_args_invalid}
    fi
}

check_package_inner_file(){
    check_file_name=`tar tvf $1 | awk '{print $6}'`
    if [[ ${check_file_name} =~ "../" || ${check_file_name} =~ "..\\" || ${check_file_name} =~ ".\\" || ${check_file_name} =~ "./" || ${check_file_name} =~ "~/" ]]; then
        logger_Warn "file name is Invalid"
        return 1
    fi
    for file_name in $check_file_name;
    do  
        if [[ $file_name != "user_config" && $file_name != "product_config" ]];then
            suffix=`echo $file_name | sed 's/.*\(....\)$/\1/'`;
            if [[ $suffix != ".key" && $suffix != ".crt" ]];then
                logger_Warn "file name suffix is Invalid ${suffix}"
                return 1
            fi
        fi
    done
    extracted_file_size=`tar tvf $1 | awk '{sum+=$3} END {print sum}'`
    if [ ${extracted_file_size} -gt ${LIMIT_SIZE_10M} ]; then
        logger_Warn "file size is too big ,size is ${extracted_file_size}"
        return 1
    fi
}

import_fd_cert(){
    ret=`bash $SCRIPT_PATH/check_FD_cert.sh $1`
    if [ $? != 0 ];then
        logger_Warn "Check FD cert info fail [${ret}]"
        return -1
    fi
    openssl x509 -in $1 -noout -subject | grep -qwi "huawei"
    if [ $? == 0 ]; then
        logger_Warn "the pre-set root certificate of Huawei is used. You are advised to replace it with the root certificate that customers trust."
    fi
    if [[ ${ret} =~ "Signature length invalid" || ${ret} =~ "Signature invalid" ]];then
        logger_Warn "FD certificates Signature algorithm or length invialid "
    fi
    logger_Info "Check FD cert info ok"

    cat $1 > ${WORK_PATH}/software/edge_core/certFD/FD.crt
    chmod 400 ${WORK_PATH}/software/edge_core/certFD/FD.crt
    sha256sum ${WORK_PATH}/software/edge_core/certFD/FD.crt > ${WORK_PATH}/software/edge_core/certFD/FD_Backup.checksum
    if [ -e /etc/redhat-release ] && [ ! -e /etc/lsb-release ]; then
        cp -f ${WORK_PATH}/software/edge_core/certFD/FD.crt /etc/pki/ca-trust/source/anchors/
        sha256sum /etc/pki/ca-trust/source/anchors/FD.crt > ${WORK_PATH}/software/edge_core/certFD/FD_Work.checksum
        update-ca-trust extract
        certPath="/etc/pki/ca-trust/source/anchors/FD.crt"
        sed -i "s|tlsCaFile:.*|tlsCaFile: ${certPath} |g" ${ATLASEDGE_CORE_CONFIG_FILE}
        logger_Info "Update FD cert info ok"
    else
        cp -f ${WORK_PATH}/software/edge_core/certFD/FD.crt /usr/local/share/ca-certificates/
        sha256sum /usr/local/share/ca-certificates/FD.crt > ${WORK_PATH}/software/edge_core/certFD/FD_Work.checksum
        update-ca-certificates
        certPath="/usr/local/share/ca-certificates/FD.crt"
        sed -i "s|tlsCaFile:.*|tlsCaFile: ${certPath} |g" ${ATLASEDGE_CORE_CONFIG_FILE}
        logger_Info "Update FD cert info ok"
    fi

    if [ -e /usr/bin/docker ]; then
        service docker restart
    fi
    logger_Info "restart docker ok"
}

check_FD_param(){
    file_size=`ls -l ${FD_CERT_IMPORT} | awk '{print $5}'`
    if [ $file_size -gt ${LIMIT_SIZE_1M} ] || [ $file_size -le 0 ];then
        logger_Warn "FD cert size over greater than limit ${file_size}"
        return ${ret_failed_import_fd_cert_failed}
    fi
    suffix=`echo ${FD_CERT_IMPORT} | sed 's/.*\(....\)$/\1/'`
    if [[ $suffix != ".crt" && $suffix != ".pem" && $suffix != ".p12" && $suffix != ".pfx" ]];then
        logger_Warn "FD cert suffix invalid"
        return ${ret_failed_import_fd_cert_failed}
    fi
}

# exit func
function exit_trap()
{
    trap - INT TERM EXIT QUIT ABRT

    if [ "${modbackpid}" !=  "" ]; then
        kill -- -$modbackpid > /dev/null 2>&1
    fi
}

main()
{
    trap exit_trap INT TERM EXIT QUIT ABRT

    #如果配置文件不存在 提示错误
    if [ ! -f ${ATLASEDGE_CONFIG_FILE} ]; then
        logger_Warn "config file not exist:[${ATLASEDGE_CONFIG_FILE}]"
        return ${ret_failed_config_file_not_exist}
    fi

    ARGS=`getopt -o h -a -l help,install_type:,manufacturer:,product_name:,netmanager:,fd_test:,fd_ip:,fd_user:,ief_cert:,fd_cert_import:,ha_work_path:,user_log_path:,collect_log_size:,ha_enable:,web_enable:,no_sys_load_mode:,whitelist:,third_hardware_info_en: -n "config.sh" -- "$@"`
    if [ $? != 0 ]
    then
        logger_Warn "Terminating....." >&2
        exit 1
    fi
    eval set -- "$ARGS"

    while true
    do
        case "$1" in
            --install_type | -install_type)
            if [ "$2" == "Full" -o "$2" == "Typical" -o "$2" == "Simple" ]; then
                INSTALL_TYPE=$2
            else
                logger_Warn "install type arg err Full Typical Simple in:" $2
                return 1;
            fi
                shift 2
                ;;
            --manufacturer | -manufacturer)
                MANUFACTURER=$2
                shift 2
                ;;
            --product_name | -product_name)
                PRODUCT_NAME=$2
                shift 2
                ;;
            --netmanager | -netmanager)
                if [ "$2" == "Web" -o "$2" == "FD" -o "$2" == "IEF" ]; then
                    NET_MANAGER=$2
                else
                    logger_Warn "net manager arg err Web FD IEF in:" $2
                    return 1;
                fi
                shift 2
                ;;
            --fd_test | -fd_test)
                if [ "$2" == "true" -o "$2" == "false" ]; then
                    FD_TEST=$2
                else
                    logger_Warn "fd_test arg err true false in:" $2
                    return 1;
                fi
                shift 2
                ;;
            --fd_ip | -fd_ip)
                FD_IP=$2
                shift 2
                ;;
            --fd_user | -fd_user)
                FD_USER=$2
                shift 2
                ;;
            --ief_cert | -ief_cert)
                IEF_CERT=$2
                shift 2
                ;;
            --fd_cert_import | -fd_cert_import)
                FD_CERT_IMPORT=$2
                shift 2
                ;;
            --ha_work_path | -ha_work_path)
                HA_WORK_PATH=$2
                shift 2
                ;;
            --user_log_path | -user_log_path)
                USER_LOG_PATH=$2
                shift 2
                ;;
            --collect_log_size | -collect_log_size)
                is_integer_check $2
                COLLECT_LOG_SIZE=$2
                shift 2
                ;;
            --no_sys_load_mode | -no_sys_load_mode)
                if [ "$2" == "1" -o "$2" == "0" ]; then
                    NO_SYS_LOAD_MODE=$2
                else
                    logger_Warn "no_sys_load mode arg err 0 1 in:" $2
                    return 1;
                fi
                shift 2
                ;;
            --ha_enable | -ha_enable)
                if [ "$2" == "1" -o "$2" == "0" ]; then
                    HA_ENABLE=$2
                else
                    logger_Warn "ha enable arg err 0 1 in:" $2
                    return 1;
                fi
                shift 2
                ;;
            --web_enable | -web_enable)
                if [ "$2" == "1" -o "$2" == "0" ]; then
                    WEB_ENABLE=$2
                else
                    logger_Warn "web enable arg err 0 1 in:" $2
                    return 1;
                fi
                shift 2
                ;;
            --third_hardware_info_en | -third_hardware_info_en)
                if [ "$2" == "1" -o "$2" == "0" ]; then
                    THIRD_HARDWARE_INFO_EN=$2
                else
                    logger_Warn "third hardware info enable arg err 0 1 in:" $2
                    return 1;
                fi
                shift 2
                ;;
            --whitelist | -whitelist)
                third_check $*
                exit 0
                ;;

            -h|--help)
                usage
                exit 1
                ;;
            --)
                shift
                break
                ;;
            *) 
                logger_Warn "Internal error!"
                exit 1
                ;;
            esac
    done

    check_args_valid
    ret=$?
    if [ ${ret} != 0 ];then
        logger_Warn "check args invalid ret:" ${ret}
        return ${ret}
    fi

    ${SCRIPT_PATH}/config_resume.sh > /dev/null 2>&1

    setsid ${SCRIPT_PATH}/mod_backup.sh > /dev/null 2>&1  &
    modbackpid=$!
    # sleep wait inotifywait run
    sleep 1

    set_config_to_file
    ret=$?
    if [ ${ret} != 0 ];then
        logger_Warn "set config to file ret:" ${ret}
        return ${ret}
    fi

    # sleep wait config backup  ok
    sleep 7
    sync
    logger_Info "config set OK"

    return ${ret_ok}
}

main "$@"
RESULT=$?
exit $RESULT
